﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ClientApplication
{
    public partial class Judge1 : Form, IJudge1
    {
        string oldId = "";
        string oldJumpNr = "";
        public event JudgeMessage ScoreButtonClicked = null;

        public Judge1()
        {
            InitializeComponent();
        }

        private void Contestant_textBox_TextChanged(object sender, EventArgs e)
        {
            EnterScore_textBox.Clear();
            CheckBoxes();
        }

        private void JumpCode_textBox_TextChanged(object sender, EventArgs e)
        {

            CheckBoxes();
        }

        private void JumpNr_textBox_TextChanged(object sender, EventArgs e)
        {
            CheckBoxes();
        }

        private void EnterScore_textBox_TextChanged(object sender, EventArgs e)
        {
            CheckBoxes();
        }

        private void SendScore_Button_Click(object sender, EventArgs e)
        {
            SendScore();
        }

        private void SendScore()
        {
            string m = EnterScore_textBox.Text;
            if (this.ScoreButtonClicked != null)
            {
                this.ScoreButtonClicked(this, m);
                EnterScore_textBox.Clear();
            }
        }

        public void HandleJudgingMessage(string contestant, string jumpCode, string jumpNr)
        {
            if (!(contestant == oldId && jumpNr == oldJumpNr))
            {
                PrintTextInTextBox(Contestant_textBox, contestant);
                PrintTextInTextBox(JumpCode_textBox, jumpCode);
                PrintTextInTextBox(JumpNr_textBox, jumpNr);
                oldId = contestant;
                oldJumpNr = jumpNr;
            }
        }

        public void HandleGeneralMessage(string message)
        {
            string[] split = message.Split('@');
            if (split[0] == "ACK")
            {
                PrintTextInTextBox(Contestant_textBox, "");
                PrintTextInTextBox(JumpCode_textBox, "");
                PrintTextInTextBox(JumpNr_textBox, "");
                message = split[1];
            }
            message = "-" + message + Environment.NewLine;

            AppendTextToTextBox(ServerResponse_textBox, message);
        }

        public bool AppendTextToTextBox(TextBox tBox, string text)
        {
            try
            {
                MethodInvoker action = delegate
                {
                    tBox.AppendText(text);
                };
                tBox.BeginInvoke(action);
                return true;
            }
            catch
            {
                MessageBox.Show("Internal error was detected");
                return false;
            }
        }

        public bool PrintTextInTextBox(TextBox tBox, string text)
        {
            try
            {
                MethodInvoker action = delegate
                {
                    tBox.Text = text;
                };
                tBox.BeginInvoke(action);
                return true;
            }
            catch
            {
                MessageBox.Show("Internal error was detected");
                return false;
            }
        }

        private void CheckBoxes()
        {
            if ((Contestant_textBox.Text.Trim().Length > 0 && JumpCode_textBox.Text.Trim().Length > 0 && JumpNr_textBox.Text.Trim().Length > 0))
            {

                if ((EnterScore_textBox.Text.Trim().Length == 3 )|| EnterScore_textBox.Text.Trim().Length == 4)
                {
                    char c1 = EnterScore_textBox.Text[0];
                    char c2 = EnterScore_textBox.Text[1];
                    char c3 = EnterScore_textBox.Text[2];
                    if ((c1 >= 48 && c1 <= 57) && (c2 == ',') && (c3 == '0' || c3 == '5') && (EnterScore_textBox.Text.Trim().Length == 3))
                    {
                        SendScore_Button.Enabled = true;
                    }
                    else if (EnterScore_textBox.Text.Trim().Length == 4)
                    {
                        char c4 = EnterScore_textBox.Text[3];
                        if ((c1 == '1') && (c2 == '0') && (c3 == ',') && (c4 == '0'))
                        {
                            SendScore_Button.Enabled = true;
                        }
                        else
                        {
                            SendScore_Button.Enabled = false;
                        }
                    }
                    else
                    {
                        SendScore_Button.Enabled = false;
                    }
                }
                else
                {
                    SendScore_Button.Enabled = false;
                }
            }
            else
            {
                SendScore_Button.Enabled = false;
            }
            if (SendScore_Button.Enabled == true)
            {
                EnterScore_textBox.BackColor = System.Drawing.Color.Lime;
            }
            else
            {
                EnterScore_textBox.BackColor = System.Drawing.Color.White;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

        }

        private void EnterScore_textBox_KeyPress_1(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
                if (SendScore_Button.Enabled == true)
                    SendScore();
        }

        private void ServerResponse_textBox_TextChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void JumpCode_Label_Click(object sender, EventArgs e)
        {

        }

        private void NumberOfJump_Label_Click(object sender, EventArgs e)
        {

        }

        private void ActivityLog_Label_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void Contestant_Label_Click(object sender, EventArgs e)
        {

        }
    }
}
